const mongoose = require('mongoose');

const logSchema = new mongoose.Schema({
  userId: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'User',
    required: true 
  },
  action: { 
    type: String,
    required: true,
    enum: ['login', 'logout', 'profile_update', 'password_change', 'security_setting']
  },
  ip: { 
    type: String,
    required: true 
  },
  userAgent: { 
    type: String,
    required: true 
  },
  details: {
    type: mongoose.Schema.Types.Mixed,
    default: {}
  },
  timestamp: { 
    type: Date, 
    default: Date.now 
  }
});

// 索引优化
logSchema.index({ userId: 1, timestamp: -1 });
logSchema.index({ action: 1, timestamp: -1 });

const Log = mongoose.model('Log', logSchema);

module.exports = Log; 